from PySide6.QtCore import Qt
from PySide6.QtWidgets import QWidget, QLabel
from qfluentwidgets import (
    FluentIcon as Fif,
    ScrollArea,
    InfoBar,
    ComboBoxSettingCard,
    SettingCardGroup,
    ExpandLayout,
    OptionsSettingCard,
    setTheme,
)

from app.common.config import cfg
from app.common.style_sheet import StyleSheet
from app.common.translator import Translator
from app.utils.log_rec import logger


class SettingInterface(ScrollArea):
    """Setting interface"""

    def __init__(self, parent=None):
        super().__init__(parent=parent)
        self.setObjectName(self.__class__.__name__)
        self.scroll_widget = QWidget()
        self.expand_layout = ExpandLayout(self.scroll_widget)
        self.translator = Translator()
        # setting label
        self.settingLabel = QLabel(self.translator.Setting, self)

        # 个性化
        self.personal_group = SettingCardGroup(
            self.translator.personalization, self.scroll_widget
        )
        # TODO:// 默认黑色主题，切换白色，部分不生效
        # TODO:// 默认白色主题，切换黑色，不分不生效
        self.theme_card = OptionsSettingCard(
            cfg.themeMode,
            Fif.BRUSH,
            self.translator.ApplicationTheme,
            self.translator.ApplicationThemeMSG,
            texts=[
                self.translator.Light,
                self.translator.Dark,
                self.translator.ThemeAuto,
            ],
            parent=self.personal_group,
        )
        self.language_card = ComboBoxSettingCard(
            cfg.language,
            Fif.LANGUAGE,
            self.translator.Language,
            self.translator.choice_language,
            texts=[
                "简体中文",
                "English",
                self.translator.LanguageAuto,
            ],
            parent=self.personal_group,
        )

        self.__init_ui()

    def __init_ui(self):
        self.resize(1000, 800)
        self.setHorizontalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAlwaysOff)
        self.setViewportMargins(0, 80, 0, 20)
        self.setWidget(self.scroll_widget)
        self.setWidgetResizable(True)
        self.setObjectName("settingInterface")

        # 初始化样式表
        self.scroll_widget.setObjectName("scrollWidget")
        self.settingLabel.setObjectName("settingLabel")
        StyleSheet.SETTING_INTERFACE.apply(self)

        self.__init_layout()
        self.__init_signal_to_slot()

    def __init_layout(self):
        """

        :return:
        """
        self.settingLabel.move(36, 30)
        self.personal_group.addSettingCard(self.language_card)
        self.personal_group.addSettingCard(self.theme_card)
        self.expand_layout.addWidget(self.personal_group)

    def __init_signal_to_slot(self):
        cfg.appRestartSig.connect(self.__show_restart_tooltip)

        self.theme_card.optionChanged.connect(
            lambda ci: setTheme(cfg.get(ci), True, True)
        )
        # self.theme_card.optionChanged.connect(self.print_component_styles)

    def __show_restart_tooltip(self):
        """show restart tooltip"""
        InfoBar.success(
            self.translator.UpdateSuccess,
            self.translator.UpdateSuccessMsg,
            duration=1500,
            parent=self,
        )

    def print_component_styles(self):
        # 获取 ScrollArea 的样式
        scroll_area_style = self.styleSheet()
        logger.debug(f"ScrollArea style: {scroll_area_style}")

        # 获取 scroll_widget 的样式
        scroll_widget_style = self.scroll_widget.styleSheet()
        logger.debug(f"scroll_widget style: {scroll_widget_style}")

        # 获取 settingLabel 的样式
        setting_label_style = self.settingLabel.styleSheet()
        logger.debug(f"settingLabel style: {setting_label_style}")

        # 获取 personal_group 的样式（可能无法直接获取，需要检查相关库是否提供获取样式表的方法）
        personal_group_style = self.personal_group.styleSheet()
        logger.debug(f"personal_group style: {personal_group_style}")

        # 获取 theme_card 的样式（可能无法直接获取，需要检查相关库是否提供获取样式表的方法）
        theme_card_style = self.theme_card.styleSheet()
        logger.debug(f"theme_card style: {theme_card_style}")

        # 获取 language_card 的样式（可能无法直接获取，需要检查相关库是否提供获取样式表的方法）
        language_card_style = self.language_card.styleSheet()
        logger.debug(f"language_card style: {language_card_style}")
